<?php
namespace app\test\controller;

use app\common\model\Business\Business;
use app\common\model\Chat;
use app\common\model\Product\Product;
use think\Cache;
use think\Controller;
use think\Queue;
use think\Request;

class Index extends Controller
{
    protected $productModel = null;
    public function __construct()
    {
        $this->productModel = new Product();
        parent::__construct();
    }

    public function push()
    {
        $delay = 30;
        $job = Action::class;
        $data = [
            'proid'=>37,
            'num'=>random_int(1,3),
        ];
        $queue = 'testqueue';
        $result = Queue::later($delay,$job,$data,$queue);
        echo $result === FALSE ? "入列失败" : date("Y-m-d H:i:s")."新任务入列成功";
    }
    public function message(Request $request)
    {
        $chatModel = new Chat();
        $businessModel = new Business();
        if($request->isPost())
        {
            
            $this->request->filter('trim,strip_tags');
            $code = $request->post('code',0);
            $result = [
                'code'=>0,
                'msg'=>''
            ];
            if($code==1){
                $sendid = $request->post('sendid',0);
                $send = $businessModel->get($sendid);
                if(!$send)return json_encode($result);
                $receiveid = $request->post('receiveid',0);
                $content = $request->post('content','');
                $params = [
                    'sendid'=>$sendid,
                    'receiveid'=>$receiveid,
                    'content'=>$content,
                ];
                $res = $chatModel->create($params,true);
                if($res===false)return json_encode($result);
                $result = [
                    'code'=>1,
                    'msg'=>'发送成功'
                ];
                return json_encode($result, JSON_UNESCAPED_UNICODE);
            }
        }else if($request->isGet())
        {
            $sendid = $request->get('sendid',0);
            $receiveid = $request->get('receiveid',0);
            $page = $request->get('page',1);
            $pageSize = 30;
            $chat = $chatModel
            ->whereOr(['sendid'=>$sendid,'receiveid'=>$receiveid])
            ->whereOr(['receiveid'=>$sendid,'sendid'=>$receiveid])
            ->order('createtime','desc')
            ->paginate($pageSize,$page);
            return $chat;
        }
    }
}
